package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.BusinessPersonMapper;
import org.linlinjava.litemall.db.dao.BusinessProjectMapper;
import org.linlinjava.litemall.db.domain.BusinessPerson;
import org.linlinjava.litemall.db.domain.BusinessPersonExample;
import org.linlinjava.litemall.db.domain.BusinessProject;
import org.linlinjava.litemall.db.domain.BusinessProjectExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/3 19:20
 * @Version: 1.0
 * @Description:
 */
@Service
public class BusinessPersonService {

    @Resource
    private BusinessPersonMapper personMapper;

    @Resource
    private BusinessProjectMapper businessProjectMapper;


    // 根据用户的名称和模块id查询出相关的项目id
    public List<String> selectOwnPorjietidsByOwnNameAndModel(String peopleName, int modelId) {
        BusinessPersonExample example = new BusinessPersonExample();
        BusinessPersonExample.Criteria criteria = example.createCriteria();
        criteria.andPersonNameEqualTo(peopleName);
        criteria.andOwningModuleEqualTo(modelId);
        // 根据自己的id查询出自己参与的所有项目，然后提取出所有项目的id，再去查询项目展示信息
        List<BusinessPerson> businessPersons =
                personMapper.selectByExampleSelective(example, BusinessPerson.Column.projectId);
        List<String> projectIds = new ArrayList<>();
        if (!CollectionUtils.isEmpty(businessPersons)){
            // 将id提取出来成为一个list
            projectIds = businessPersons.stream().map(BusinessPerson::getProjectId).collect(Collectors.toList());
        }

        //BusinessProjectExample businessProjectExample = new BusinessProjectExample();
        //BusinessProjectExample.Criteria criteria1 = businessProjectExample.createCriteria();
        //criteria1.andAdminNameEqualTo(peopleName);
        //List<BusinessProject> projectList = businessProjectMapper.selectByExampleSelective(businessProjectExample, BusinessProject.Column.projectId);
        //if (!CollectionUtils.isEmpty(projectList)){
        //    // 将id提取出来成为一个list
        //    projectIds = projectList.stream().map(BusinessProject::getProjectId).collect(Collectors.toList());
        //}
        if (!CollectionUtils.isEmpty(projectIds)){
            // 将id提取出来成为一个list
            return projectIds;
        }
        return null;
    }

    // 根据项目id删除项目成员信息
    public int deleteByProjectId(String projectId) {
        BusinessPersonExample example = new BusinessPersonExample();
        BusinessPersonExample.Criteria criteria = example.createCriteria();
        criteria.andProjectIdEqualTo(projectId);
        return personMapper.deleteByExample(example);
    }

    public int insert(BusinessPerson businessPerson) {
        return personMapper.insert(businessPerson);
    }

    public List<BusinessPerson> selectByProjectId(String projectId) {
        BusinessPersonExample example = new BusinessPersonExample();
        BusinessPersonExample.Criteria criteria = example.createCriteria();
        criteria.andProjectIdEqualTo(projectId);
        example.orderBy("person_rank");
        return personMapper.selectByExample(example);
    }

    // 根据主键进行更新
    public int updateByPrimaryId(BusinessPerson businessPerson) {
        return personMapper.updateByPrimaryKey(businessPerson);
    }

    // 查询贡献
    public List<BusinessPerson> selectPerformanceByDuty(Integer dutyCode, String peopleName,Integer modelId) {
        BusinessPersonExample example = new BusinessPersonExample();
        BusinessPersonExample.Criteria criteria = example.createCriteria();
        criteria.andPersonNameEqualTo(peopleName);
        criteria.andPersonDutyEqualTo(dutyCode);
        if (modelId != null){
            criteria.andOwningModuleEqualTo(modelId);
        }
        example.orderBy("project_name");
        return personMapper.selectByExample(example);
    }
}
